The Spreadsheet Workbench allows you to create and edit spreadsheets, use data from the spreadsheet as parameters in a model, fill the spreadsheet with data retrieved from a model, perform calculations, and export the data to other spreadsheet applications such as LibreOffice or Microsoft Excel.
A spreadsheet with certain cells filled with text and quantities
Note that deleting or removing cells with data can break the spreadsheet and your model if it relies on the spreadheet. You are not prewarned if this happens.
Rows and columns can be inserted or removed by right-clicking a row or column header and selecting the appropriate option from the contex menu. It is possible to select multiple rows or columns first. Either by holding down the Ctrl key while selecting the headers, or by holding down the left mouse button and dragging.
The content of a cell can be edited by selecting the cell and entering a value in the Content inputbox at the top of the window. To edit a cell in-place, select it and press F2, or double-click it.
To delete one or more cells select them and press Del. This will delete their contents, their properties and their aliases. To only delete the content of a cell it should be edited instead.
Cut and copy-paste operations can be used on spreadsheets cells. You can use the normal shortcuts for these operations: Ctrl+X, Ctrl+C and Ctrl+V respectively. To select multiple cells hold down the Ctrl key while selecting, or hold down the left mouse button and drag to select a rectangular cell range.
The cut and copy operations store the contents, properties and aliases of the cells on the Clipboard. The paste operation writes the data in such a way that the content of the top left cell of the stored data is dropped in the active cell. Other stored content is placed relative to that cell. Formulas are updated accordingly. Aliases are only pasted if they are unique.
The properties of a spreadsheet cell can be edited by right-clicking the cell and selecting Properties... from the context menu. The following dialog pops up:
As indicated by the tabs, the following properties can be changed:
A spreadsheet cell may contain a number, a text or an expression. Expressions must start with an equal sign '='.
Cell expressions may contain numbers, functions, references to other cells, and references to properties of the model (But see Current limitations below). A cell can be referenced by its address (CAPITAL column letter + row number, e.g. B4) or by its alias.
Note: Cell expressions are treated by FreeCAD as programming code. Therefore, when you edit a cell the content you see may not follow your display settings:
References to objects in the model are explained under References to CAD-data below. Using spreadsheet cell values to define model properties are explained under Spreadsheet data in expressions below. For more information on expressions and the available functions, see Expressions.
Data in the cells of a spreadsheet may be used in CAD model parameter expressions. Thus, a spreadsheet may be used as the source for parameter values used throughout a model, effectively gathering the values in one place. When values are changed in the spreadsheet, they are propagated throughout the model.
Similarly, properties from CAD model objects may be used in expressions in spreadsheet cells. This allows use of object properties like volume or area in the spreadsheet. If the name of an object in the CAD model is changed, the change will automatically be propagated to any references in spreadsheet expressions using the name which was changed.
More than one spreadsheet may be used in a document. A spreadsheet can be identified using either its name or its label.
FreeCAD will automatically assign a unique name to a spreadsheet when it is created. These names follow the pattern Spreadsheet
, Spreadsheet001
, Spreadsheet002
and so on. The name can not be changed, and it is not visible in the properties of the spreadsheet. It can be used to refer to the spreadsheet in an Expression (see Spreadsheet data in expressions below.)
The label of a spreadsheet is automatically set to the name of the spreadsheet upon creation. Unlike the name, the label can be changed, for example in the properties panel or using the context menu action Rename. By default FreeCAD does not accept duplicate labels, but there is a preference to override this. Spreadsheets with duplicate labels in the same document cannot be referenced by their label.
FreeCAD checks for cyclic dependencies. See Current limitations.
As indicated above, one can reference data from the CAD model in spreadsheet expressions.
The following table shows some examples assuming the model has a feature named "MyCube":
CAD-Data | Cell in Spreadsheet | Result |
---|---|---|
Parametric Length of a Part-Workbench Cube | =MyCube.Length
|
Length with units mm |
Volume of the Cube | =MyCube.Shape.Volume
|
Volume in mm³ without units |
Type of the Cube-shape | =MyCube.Shape.ShapeType
|
String: Solid |
Label of the Cube | =MyCube.Label
|
String: MyCube |
X coordinate of the center of mass of the Cube | =MyCube.Shape.CenterOfMass.x
|
Coordinate in mm without units |
In order to use spreadsheet data in other parts of FreeCAD, you will usually create an Expression that refers to the spreadsheet and the cell that contains the data you want to use. You can identify spreadsheets by name or by label, and you can identify the cells by address or by alias. Autocompletion is available for all forms of referencing.
Spreadsheet by Name | Spreadsheet by Label | |
---|---|---|
Cell by Address | =Spreadsheet042.B5
|
=<<MySpreadsheet>>.B5
|
Cell by Alias | =Spreadsheet042.MyAlias
|
=<<MySpreadsheet>>.MyAlias
|
The recommended way to refer to spreadsheet data is to use the spreadsheet label and cell alias name. For a more in-depth explanation of the pros and cons of the referencing modes, see the expanded section below.
Using the spreadsheet label has the advantage that it can be freely changed to describe the contents of the spreadsheet. It is also easier to identify the spreadsheet that is being used since the text in the expression matches the label shown in the model and property views. If you decide to change the label of a spreadsheet, existing references to the contents of the spreadsheet will be updated, so you won't break your expressions by renaming the spreadsheet. The internal name of the spreadsheet is not readily available anywhere except within the expression editor, so if you use the internal name and later decide to rename the spreadsheets, you might have a hard time tracing your expression data back to its source.
Be aware that when you create a new spreadsheet, the name and the label are the same, so it is easy to accidentally use the spreadsheet name instead of the label. A simple way to avoid this is to give the spreadsheet a meaningful name before starting to use it in expressions.
While you may use the row and column number in an expression to reference a cell, best practice is to give the cell an alias name and use that. See Cell properties on how to set the alias. For example, if the data in cell B1 contained the length parameter for an object, an alias name of MyObject_Length
would allow the value to be referred to as <<MyParams>>.MyObject_Length
instead of Spreadsheet.B1
. Besides being much easier to read and understand, alias names are also much easier to change if you decide to adjust the structure of your spreadsheet. Using an alias also has the advantage that it is reasier to see which cells are used to control other parts of the document. Note that FreeCAD will automatically adjust the positional references in expressions if you insert or remove rows and columns in the spreadsheet, so even if you use row and column numbers in an expression, you can insert rows and columns without breaking the references to the surrounding cells.
Editing a spreadsheet will trigger a recompute of the 3D model, even if the changes do not affect the model. For a complex model a recompute can take a long time, and having to wait after every single edit is of course quite annoying.
There are three solutions to deal with this:
#PENDING
is shown.=NameOfFile#<<MySpreadsheet>>.MyAlias
.The Spreadsheet has a notion of dimension (units) associated with cell values. A number entered without an associated unit has no dimension. The unit should be entered immediately following the number value, with no intervening space. If a number has an associated unit, that unit will be used in all calculations. For example, the multiplication of two lengths with the unit mm gives an area with the unit mm².
If a cell contains a value which represents a dimension, it should be entered with its associated unit. While in many simple cases one can get by with a dimensionless value, it is unwise to not enter the unit. If a value representing a dimension is entered without its associated unit, there are some sequences of operations which cause FreeCAD to complain of incompatible units in an expression when it appears the expression should be valid. (This may be better understood by viewing this thread in the FreeCAD forums.)
You can change the units displayed for a cell value using the Cell properties dialog. This does not change the value contained in the cell; it only converts the existing value for display. The value used for calculations does not change, and the results of formulas using the value do not change. For example, a cell containing the value "5.08cm" can be displayed as "2in" by changing the units tab value to "in".
A dimensionless number cannot be changed to a number with a unit by the cell properties dialog. One can put in a unit string, and that string will be displayed; but the cell still contains a dimensionless number. In order to change a dimensionless value to a value with a dimension, the value itself must be re-entered with its associated unit.
Occasionally it may be desirable to get rid of a dimension in an expression. This can be done by multiplying by 1 with a reciprocal unit.
FreeCAD spreadsheets can be imported and exported to the CSV format which can also be read and written by most other spreadsheet applications such as Microsoft Excel or LibreOffice Calc. See Spreadsheet Import and Spreadsheet Export for more information.
Spreadsheets in the Excel-format XLSX can be imported with the Std Import command or the Std Open command. The following features are supported:
Other functionality is not imported into the FreeCAD spreadsheet.
To handle the page setup necessary for printing, FreeCAD spreadsheets are printed by inserting them into a TechDraw Spreadsheet View.
FreeCAD checks for cyclic dependencies when it recomputes. By design, that check stops at the level of the spreadsheet object. As a consequence, you should not have a spreadsheet which contains both cells whose values are used to specify parameters to the model, and cells whose values use output from the model. For example, you cannot have cells specifying the length, width, and height of an object, and another cell which references the total volume of the resulting shape. This restriction can be surmounted by having two spreadsheets: one used as a data-source for input parameters to the model and the other used for calculations based on resultant geometry-data.
It is possible to bind the content of cells to other spreadsheet cells. This can be useful when dealing with large tables or to get cell content from another spreadsheet.
To bind, for example, the cell range A3-C4 to the cell range B1-D2:
The spreadsheet may now look like this
You can use Spreadsheets to create configuration tables with sets of predefined parameters for your model, and then dynamically change which configuration to use. See the Configuration Tables tutorial. Read this Forum post if you want to know more about the inner workings of this feature.
import Spreadsheet
sheet = App.ActiveDocument.addObject("Spreadsheet::Sheet", "MySpreadsheet")
sheet.Label = "Dimensions"
sheet.set("A1", "10mm")
sheet.recompute()
sheet.get("A1")
sheet.setAlias("B1", "Diameter")
sheet.set("Diameter", "20mm")
sheet.recompute()
sheet.get("Diameter")
# sheet.get() results in an error if the cell is empty.
# sheet.getContents() can be used to check the cell first.
if sheet.getContents("C1"):
print(sheet.get("C1"))